/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Apr 26, 2012  11:21:32 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.sales.booking
 * File Name : SalesBookingProcessing.java
 * TODO
 */
package apps.business.process.sales.booking;

import java.util.ArrayList;
import java.util.List;
import jpa.entitys.synchro.staging.FiArreceiptreOutb;
import jpa.entitys.synchro.staging.PsWbsMaster;
import jpa.entitys.synchro.staging.SdBookContrOutb;
import jpa.entitys.synchro.staging.SdChargecode;
import jpa.entitys.synchro.staging.SdCustomerOutb1;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import sap.business.fi.outbound.FiArreceiptreOutbService;
import sap.business.ps.outbound.PsWbsMasterService;
import sap.business.sd.outbound.SdChargecodeService;
import sap.business.sd.outbound.VwSyncMktCustBookContService;
import sap.business.sd.outbound.VwSyncMktCustomerService;
import apps.promis.out.common.PropertyUnitAction;
import apps.promis.out.common.VwSyncRcvdInstallmentAction;
import apps.promis.out.sales.ReceiptedChargeCodeAction;
import apps.promis.out.sales.VwSyncMktBookConstructionAction;
import apps.promis.out.sales.VwSyncMktCustomerAction;

/**
 * @author chatree
 *
 */
public class SalesBookingProcessing {
	
	private static final Logger log = Logger.getLogger(SalesBookingProcessing.class);
	
	public List<SdBookContrOutb> newProcessingSingleForRMS(String company, String project, String BookingOrContarctNo, String status, String syncIsstaging, String issync) throws Exception
	{
		try{			
			
			//Booking or Contract ===================================================================================
			System.out.println("Searching...Sales Bookiong or Contarct [ company="+ company +" || project="+ project +" || BookingOrContarctNo="+ status + " || syncIsstaging=" + syncIsstaging +" || issync="+ issync +"] --->");
			VwSyncMktBookConstructionAction vwSyncMktBookConstructionAction = new VwSyncMktBookConstructionAction();
			
			List<SdBookContrOutb> sdbooking_list = vwSyncMktBookConstructionAction.CreateToStagingOnly(company, project, BookingOrContarctNo, status, syncIsstaging);
			System.out.println("Created Sales Booking or Contract [ company="+ company +" || project="+ project +" || BookingOrContarctNo="+ status + " || syncIsstaging=" + syncIsstaging +" || issync="+ issync +"] to Staging--->");					

			if(!CommonService.IsNull(sdbooking_list)){
				
				//Start Property Unit =======================================================================================
				//ssPropertyUnitAction propertyUnitAction = new PropertyUnitAction();
				
				//log.info("Searching Property Unit "+ company + "||" + project + "||" + sdbooking_list.get(0).getBuildingPhaseCode() + "||" + sdbooking_list.get(0).getFloorZone() + "||" + sdbooking_list.get(0).getUnitNumber() +" from BS,CS --->");
				//List<PsWbsMaster> unitLst = propertyUnitAction.CreatedToStaging(company, project, sdbooking_list.get(0).getBuildingPhaseCode(), sdbooking_list.get(0).getFloorZone(), sdbooking_list.get(0).getUnitNumber());
				
				//if(!CommonService.IsNull(unitLst)){
					//log.info("Create Property Unit "+ company + "||" + project + "||" + unitLst.get(0).getBuildingPhase() + "||" + unitLst.get(0).getFloorZone() + "||" + unitLst.get(0).getUnitNumber() +" --->");
					
					//new PsWbsMasterService().SendingToSAP(unitLst);
					//log.info("Sending Property Unit "+ company + "||" + project + "||" + unitLst.get(0).getBuildingPhase() + "||" + unitLst.get(0).getFloorZone() + "||" + unitLst.get(0).getUnitNumber() +" from BS,CS to SAP --->");
					//End Property Unit =======================================================================================
					
					new VwSyncMktCustBookContService().SendingToSAP(sdbooking_list, issync);
					System.err.println("Sending Sales Booking or Contract : " + company + "||" + project + "||" + sdbooking_list.get(0).getDocumentNo() + "||" + status +" to SAP --->");
					
					return sdbooking_list;
					
				//}else
				//	log.error("find not found(s) Property Unit ["+ company + "||" + project + "||" + sdbooking_list.get(0).getBuildingPhaseCode() + "||" + sdbooking_list.get(0).getFloorZone() + "||" + sdbooking_list.get(0).getUnitNumber() +"]");
				
			}else
				System.err.println("Can't Sending Booking or Contract ( "+ company + "||" + project + "||" + BookingOrContarctNo + "||" + status + "||" + syncIsstaging + "||" + issync +" ) to SAP-->.");
						
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
		
		return null;
	}	
	
	/**
	 * 		String company = "SP";
			String project = "SPLCITY-SRI";
			String BookingOrContarctNo = "CS-1205-000006-0"; //  "CS-1204-000009-0" // "BS-1204-000007"; //BS-1204-000016
			String status = "A";
			String syncIsstaging = "Y"; //Can set value to [null]
			String issync = "N";
	 * @param company
	 * @param project
	 * @param BookingOrContarctNo
	 * @param status
	 * @param syncIsstaging
	 * @param issync
	 * @return
	 * @throws Exception
	 */
	public List<SdBookContrOutb> newProcessingSingle(String company, String project, String BookingOrContarctNo, String status, String syncIsstaging, String issync) throws Exception
	{
		try{
			
			//Booking or Contract ===================================================================================			
			List<SdBookContrOutb> sdbooking_list = new VwSyncMktBookConstructionAction().CreateToStagingOnly(company, project, BookingOrContarctNo, status, syncIsstaging);
			System.out.println("Sales Booking or Contract [ company="+ company +" || project="+ project +" || BookingOrContarctNo="+ BookingOrContarctNo + " || syncIsstaging=" + syncIsstaging +" || issync="+ issync +"] to Size="+ CommonService.getSize(sdbooking_list) +" Staging--->");

			if(CommonService.getSize(sdbooking_list) > 0){
				SdBookContrOutb sdbookingContrOutb = sdbooking_list.get(0);
				
				//Start Property Unit =======================================================================================			
				List<PsWbsMaster> unitLst = new PropertyUnitAction().CreatedToStaging(company, project, sdbooking_list.get(0).getBuildingPhaseCode(), sdbooking_list.get(0).getFloorZone(), sdbooking_list.get(0).getUnitNumber());
				System.out.println("Property Unit "+ company + "||" + project + "||" + sdbooking_list.get(0).getBuildingPhaseCode() + "||" + sdbooking_list.get(0).getFloorZone() + "||" + sdbooking_list.get(0).getUnitNumber() +" from BS,CS size="+ CommonService.getSize(unitLst) +" --->");
				
				if(!CommonService.IsNull(unitLst)){
					new PsWbsMasterService().SendingToSAP(unitLst);
					System.out.println("Sending Property Unit "+ company + "||" + project + "||" + unitLst.get(0).getBuildingPhase() + "||" + unitLst.get(0).getFloorZone() + "||" + unitLst.get(0).getUnitNumber() +" from BS,CS to SAP Size="+ CommonService.getSize(unitLst) +"--->");
					//End Property Unit =======================================================================================
				}else{
					System.out.println("find not found(s) Property Unit ["+ company + "||" + project + "||" + sdbooking_list.get(0).getBuildingPhaseCode() + "||" + sdbooking_list.get(0).getFloorZone() + "||" + sdbooking_list.get(0).getUnitNumber() +"]");
				}				
				
				
				/*
				 * DR.TREE STOP 2013-01-17
				 */
				try{
					//Customer *****************
					List<String> customer_lst = new VwSyncMktBookConstructionAction().CustomerWithJoint(sdbookingContrOutb);
					System.out.println("Find CustomerWithJoint from BS,CS ("+BookingOrContarctNo+") Size="+ CommonService.getSize(customer_lst) +" ");
					if(!CommonService.IsNull(customer_lst)){
						for(String customerCode : customer_lst)
						{
							List<SdCustomerOutb1> sdCustomerLst = new VwSyncMktCustomerAction().CreateToStaging(customerCode);
							System.out.println("Create Customer from BS,CS ("+BookingOrContarctNo+") CustomerCode=" + customerCode + " || Size="+ CommonService.getSize(sdCustomerLst) +" to Staging size="+ CommonService.getSize(sdCustomerLst) +"--->");

								if(CommonService.getSize(sdCustomerLst) > 0)
								{
									new VwSyncMktCustomerService().SendingToSAP(sdCustomerLst);
									log.info("Sending Customer : " + sdCustomerLst.get(0).getCustomerCode() + " from BS,CS ("+BookingOrContarctNo+") to SAP size="+ CommonService.getSize(sdCustomerLst) +" --->");
									System.out.println("Sending Customer : " + sdCustomerLst.get(0).getCustomerCode() + " from BS,CS ("+BookingOrContarctNo+") to SAP size="+ CommonService.getSize(sdCustomerLst) +" --->");									
								}
						}
					}
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage());
					System.err.println(e.getMessage());
				}
				
				new VwSyncMktCustBookContService().SendingToSAP(sdbooking_list, issync);
				System.out.println("Sending Sales Booking or Contract : " + company + "||" + project + "||" + sdbooking_list.get(0).getDocumentNo() + "||" + status +" to SAP Size="+ CommonService.getSize(sdbooking_list) +"--->");
				
				return sdbooking_list;				
			}else{
				System.out.println("Can't Sending Booking or Contract ( "+ company + "||" + project + "||" + BookingOrContarctNo + "||" + status + "||" + syncIsstaging + "||" + issync +" ) to SAP-->.");
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
		return null;
	}
	
	public void SendAllWithNonSending() throws Exception
	{
		try{
			
			List<Object[]> lst = new VwSyncMktBookConstructionAction().SearchAllWithNonSending();
			System.out.println("SearchAllWithNonSending size=" + CommonService.getSize(lst));
			
			if(CommonService.getSize(lst) > 0){
				
				for(Object[] value : lst){
					
					String company = value[0].toString();
					String project = value[1].toString();
					String BookingOrContarctNo  = value[2].toString();
					String status = value[3].toString();
					String syncIsstaging = value[4].toString();
					String issync = "N";
					
					//Booking or Contract ===================================================================================
					System.out.println("Searching...Sales Bookiong or Contarct [ company="+ company +" || project="+ project +" || BookingOrContarctNo="+ BookingOrContarctNo + " || Status="+ status + " || syncIsstaging=" + syncIsstaging +" || issync="+ issync +"] --->");
					
					List<SdBookContrOutb> sdbooking_list = new VwSyncMktBookConstructionAction().CreateToStagingOnly(company, project, BookingOrContarctNo, status, syncIsstaging);
					System.out.println("Created Sales Booking or Contract [ company="+ company +" || project="+ project +" || BookingOrContarctNo="+ BookingOrContarctNo + "  || Status="+ status + " || syncIsstaging=" + syncIsstaging +" || issync="+ issync +"] to size="+ CommonService.getSize(sdbooking_list) +" Staging--->");

					if(!CommonService.IsNull(sdbooking_list)){
						
						//Start Property Unit =======================================================================================
						PropertyUnitAction propertyUnitAction = new PropertyUnitAction();
						
						System.out.println("Searching Property Unit "+ company + "||" + project + "||" + sdbooking_list.get(0).getBuildingPhaseCode() + "||" + sdbooking_list.get(0).getFloorZone() + "||" + sdbooking_list.get(0).getUnitNumber() +" from BS,CS --->");
						List<PsWbsMaster> unitLst = propertyUnitAction.CreatedToStaging(company, project, sdbooking_list.get(0).getBuildingPhaseCode(), sdbooking_list.get(0).getFloorZone(), sdbooking_list.get(0).getUnitNumber());
						
						if(!CommonService.IsNull(unitLst)){
							System.out.println("Create Property Unit "+ company + "||" + project + "||" + unitLst.get(0).getBuildingPhase() + "||" + unitLst.get(0).getFloorZone() + "||" + unitLst.get(0).getUnitNumber() +" Size="+ CommonService.getSize(unitLst) +" --->");
							
							new PsWbsMasterService().SendingToSAP(unitLst);
							System.out.println("Sending Property Unit "+ company + "||" + project + "||" + unitLst.get(0).getBuildingPhase() + "||" + unitLst.get(0).getFloorZone() + "||" + unitLst.get(0).getUnitNumber() +" from BS,CS to SAP --->");
							
							new VwSyncMktCustBookContService().SendingToSAP(sdbooking_list, issync);
							System.out.println("Sending Sales Booking or Contract : " + company + "||" + project + "||" + sdbooking_list.get(0).getDocumentNo() + "||" + status +" to SAP Size="+ CommonService.getSize(sdbooking_list) +"--->");
							
						}else{
							System.out.println("find not found(s) Property Unit ["+ company + "||" + project + "||" + sdbooking_list.get(0).getBuildingPhaseCode() + "||" + sdbooking_list.get(0).getFloorZone() + "||" + sdbooking_list.get(0).getUnitNumber() +"]");					
						}
					}else{
						System.out.println("Can't Sending Booking or Contract ( "+ company + "||" + project + "||" + BookingOrContarctNo + "||" + status + "||" + syncIsstaging + "||" + issync +" ) to SAP-->.");
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	public List<SdBookContrOutb> ProcessingInStagingAllSingle(String ISSYNC, String SYNC_CREATEDON) throws Exception
	{
		VwSyncMktBookConstructionAction vwSyncMktBookConstructionAction = new VwSyncMktBookConstructionAction();
		try{			
			
			List<Object[]> lst = vwSyncMktBookConstructionAction.SearchUnitAllInStagingIsSync(ISSYNC, SYNC_CREATEDON);
			System.out.println("SearchUnitAllInStagingIsSync size=" + CommonService.getSize(lst));
			
			if(CommonService.getSize(lst) > 0){
				
				for(Object[] value : lst){
					
					String company = value[0].toString();
					String project = value[1].toString();
					String BookingOrContarctNo  = value[2].toString();
					String status = value[3].toString();
					String syncIsstaging = value[4].toString();
					String issync = "N";
					
					//Booking or Contract ===================================================================================
					System.out.println("Searching...Sales Bookiong or Contarct [ company="+ company +" || project="+ project +" || BookingOrContarctNo="+ status + " || syncIsstaging=" + syncIsstaging +" || issync="+ issync +"] --->");
					
					
					List<SdBookContrOutb> sdbooking_list = vwSyncMktBookConstructionAction.CreateToStagingOnly(company, project, BookingOrContarctNo, status, syncIsstaging);
					System.out.println("Created Sales Booking or Contract [ company="+ company +" || project="+ project +" || BookingOrContarctNo="+ status + " || syncIsstaging=" + syncIsstaging +" || issync="+ issync +"] to Staging--->");					

					if(!CommonService.IsNull(sdbooking_list)){
						
						//Start Property Unit =======================================================================================
						PropertyUnitAction propertyUnitAction = new PropertyUnitAction();
						
						System.out.println("Searching Property Unit "+ company + "||" + project + "||" + sdbooking_list.get(0).getBuildingPhaseCode() + "||" + sdbooking_list.get(0).getFloorZone() + "||" + sdbooking_list.get(0).getUnitNumber() +" from BS,CS --->");
						List<PsWbsMaster> unitLst = propertyUnitAction.CreatedToStaging(company, project, sdbooking_list.get(0).getBuildingPhaseCode(), sdbooking_list.get(0).getFloorZone(), sdbooking_list.get(0).getUnitNumber());
						
						if(!CommonService.IsNull(unitLst)){
							System.out.println("Create Property Unit "+ company + "||" + project + "||" + unitLst.get(0).getBuildingPhase() + "||" + unitLst.get(0).getFloorZone() + "||" + unitLst.get(0).getUnitNumber() +" --->");
							
							new PsWbsMasterService().SendingToSAP(unitLst);
							System.out.println("Sending Property Unit "+ company + "||" + project + "||" + unitLst.get(0).getBuildingPhase() + "||" + unitLst.get(0).getFloorZone() + "||" + unitLst.get(0).getUnitNumber() +" from BS,CS to SAP --->");
							//End Property Unit =======================================================================================
							
							new VwSyncMktCustBookContService().SendingToSAP(sdbooking_list, issync);
							System.out.println("Sending Sales Booking or Contract : " + company + "||" + project + "||" + sdbooking_list.get(0).getDocumentNo() + "||" + status +" to SAP --->");
							
							return sdbooking_list;
							
						}else
							System.err.println("find not found(s) Property Unit ["+ company + "||" + project + "||" + sdbooking_list.get(0).getBuildingPhaseCode() + "||" + sdbooking_list.get(0).getFloorZone() + "||" + sdbooking_list.get(0).getUnitNumber() +"]");
						
					}else
						System.err.println("Can't Sending Booking or Contract ( "+ company + "||" + project + "||" + BookingOrContarctNo + "||" + status + "||" + syncIsstaging + "||" + issync +" ) to SAP-->.");
				}
			}					
						
		}catch(Exception e){
			e.printStackTrace();
			System.err.println(e.getMessage());
			throw e;
		}
		
		return null;
	}
	
	public List<SdBookContrOutb> ProcessingNotSyncInStaging() throws Exception
	{
		try{			
			
			List<Object[]> lst =  new VwSyncMktBookConstructionAction().SearchNotSyncInStaging();
			System.out.println("SearchNotSyncInStaging size=" + CommonService.getSize(lst));
			
			if(CommonService.getSize(lst) > 0){
				for(Object[] value : lst){
					
					try{
						
						String documentNumber = value[3].toString();
						String instanceNo = value[5].toString();					
											
						System.out.println("Searching...Sales Bookiong or Contarct [documentNumber="+ documentNumber +" || instanceNo="+ instanceNo + "] --->");											
						List<SdBookContrOutb> sdbooking_list =  new VwSyncMktBookConstructionAction().findByInstanceNo(documentNumber, instanceNo);
						
						//Send to SAP
						new VwSyncMktCustBookContService().SendingToSAP(sdbooking_list, "N");						
						System.out.println("ProcessingNotSyncInStaging sending to SAP size=" + CommonService.getSize(sdbooking_list));	
						
					}catch(Exception e){
						e.printStackTrace();
						log.error(e.getMessage());
						System.err.println(e.getMessage());
					}					
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			System.err.println(e.getMessage());
			throw e;
		}
		return null;
	}
	
	/**
	 * 		String company = "SP";
			String project = "SPLCITY-SRI";
			String BookingOrContarctNo = "CS-1205-000006-0"; //  "CS-1204-000009-0" // "BS-1204-000007"; //BS-1204-000016
			String ReceipteddocumentNo = "RR-1205-000024";
			String receipt_status = "A";
			String bs_cs_status_opt = NULL;
			String syncIsstaging = "Y"; //Can set value to [null]
			String issync = "N";
	 * @param company
	 * @param project
	 * @param BookingOrContarctNo
	 * @param ReceipteddocumentNo
	 * @param status
	 * @param syncIsstaging
	 * @param issync
	 * @throws Exception
	 */
	public void newProcessing(String company, String project, String BookingOrContarctNo, String ReceipteddocumentNo, String receipt_status, String bs_cs_status_opt, String syncIsstaging_opt, String issync) throws Exception 
	{
		System.out.println("Start Sales Booking or Contract Processing ...newProcessing ---> total step 1#Send Unit of Booking  2#Send Booking or Contract 3#Send Receipt and ChargeCode ");
		System.out.println("[company =" + company + " || project=" + project + " || BookingOrContarctNo="+ BookingOrContarctNo + " || ReceipteddocumentNo=" + ReceipteddocumentNo + " || receipt_status="+ receipt_status +" || bs_cs_status_opt="+ bs_cs_status_opt +" || syncIsstaging="+ syncIsstaging_opt +"]");
		
		String buildingPhase = null;
		String floorZone  = null;
		String unitNumber  = null;
		try{
			//Sales Booking, Contract***************
			List<SdBookContrOutb> sdbooking_list = new VwSyncMktBookConstructionAction().CreateToStagingOnly(company, project, BookingOrContarctNo, null);
			System.out.println("Created Sales Booking or Contract to Staging Size="+ CommonService.getSize(sdbooking_list)+ "--->");
			
			if(!CommonService.IsNull(sdbooking_list)){
				
				SdBookContrOutb sdbookingContrOutb = sdbooking_list.get(0);
				floorZone = sdbookingContrOutb.getFloorZone();
				buildingPhase = sdbookingContrOutb.getBuildingPhaseCode();
				unitNumber = sdbookingContrOutb.getUnitNumber();
				
				try{
					//Property Unit*****************************
					List<PsWbsMaster> unitLst = new PropertyUnitAction().CreatedToStaging(company, project, buildingPhase, floorZone, unitNumber);
					System.out.println("Create Unit [company="+company+" || project="+project+" || buildingPhase="+buildingPhase+" floorZone="+floorZone+" unitNumber="+unitNumber+"] to from BS,CS ("+BookingOrContarctNo+") || Size="+ CommonService.getSize(unitLst) +"] Size="+ CommonService.getSize(unitLst)+" Staging --->");
					
					if(!CommonService.IsNull(unitLst)){
						new PsWbsMasterService().SendingToSAP(unitLst);
						System.out.println("Step 1# Sending Unit [Company="+ unitLst.get(0).getCompany() + "|| Project=" + unitLst.get(0).getProject() + "|| BuildingPhase=" + unitLst.get(0).getBuildingPhase() + "|| FloorZone=" + unitLst.get(0).getFloorZone() + "|| UnitNumber=" + unitLst.get(0).getUnitNumber() +" || UnitStatus="+ unitLst.get(0).getSalesStatusName() +" from BS,CS ("+BookingOrContarctNo+") || Size="+ CommonService.getSize(unitLst) +"] to SAP Size="+ CommonService.getSize(unitLst) +" --->");
					}
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage());
					System.err.println(e.getMessage());
				}
				
				/*
				 * DR.TREE STOP 2013-01-17
				 */
				try{
					//Customer *****************
					List<String> customer_lst = new VwSyncMktBookConstructionAction().CustomerWithJoint(sdbookingContrOutb);
					System.out.println("Find CustomerWithJoint from BS,CS ("+BookingOrContarctNo+") Size="+ CommonService.getSize(customer_lst) +" ");
					if(!CommonService.IsNull(customer_lst)){
						for(String customerCode : customer_lst)
						{
							List<SdCustomerOutb1> sdCustomerLst = new VwSyncMktCustomerAction().CreateToStaging(customerCode);
							System.out.println("Create Customer from BS,CS ("+BookingOrContarctNo+") CustomerCode=" + customerCode + " || Size="+ CommonService.getSize(sdCustomerLst) +" to Staging size="+ CommonService.getSize(sdCustomerLst) +"--->");

								if(CommonService.getSize(sdCustomerLst) > 0)
								{
									new VwSyncMktCustomerService().SendingToSAP(sdCustomerLst);
									log.info("Sending Customer : " + sdCustomerLst.get(0).getCustomerCode() + " from BS,CS ("+BookingOrContarctNo+") to SAP size="+ CommonService.getSize(sdCustomerLst) +" --->");
									System.out.println("Sending Customer : " + sdCustomerLst.get(0).getCustomerCode() + " from BS,CS ("+BookingOrContarctNo+") to SAP size="+ CommonService.getSize(sdCustomerLst) +" --->");									
								}
						}
					}
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage());
					System.err.println(e.getMessage());
				}
				
				
				//Send Booking to SAP **************				
				new VwSyncMktCustBookContService().SendingToSAP(sdbooking_list, issync);
				System.out.println("Step 2# Sending Sales Booking or Contract="+ sdbookingContrOutb.getDocumentNo() +" || Size="+ CommonService.getSize(sdbooking_list) +" to SAP size="+ CommonService.getSize(sdbooking_list) +"--->");
				
					//Receipted ************************				
					if(!CommonService.IsNull(ReceipteddocumentNo)){
						try{
							List<FiArreceiptreOutb> rcvdLst = new VwSyncRcvdInstallmentAction().CreateRcvdToStaging(company, project, ReceipteddocumentNo, receipt_status, syncIsstaging_opt);
							System.out.println("Create FiArreceiptreOutb of installment [company="+ company +", project="+ project +", ReceipteddocumentNo="+ ReceipteddocumentNo +", receipt_status="+ receipt_status +", syncIsstaging="+ syncIsstaging_opt +"] to Staging Size=" + CommonService.getSize(rcvdLst));
							
							if(!CommonService.IsNull(rcvdLst)){
								//Create ChargeCode To Staging			
							    List<SdChargecode> SdChargecodeList = new ReceiptedChargeCodeAction().CreateToStaging(rcvdLst);
							    System.out.println("Create ChargeCode Size="+ CommonService.getSize(SdChargecodeList) +" --->");
								
							    if(CommonService.getSize(SdChargecodeList) > 0){
							    	//Sending ChargeCode to SAP
							    	SdChargecodeService object = new SdChargecodeService();
							    	object.SendingToSAP(SdChargecodeList);
							    	object = null;
							    	System.out.println("Step 3# Sending SdChargeCode [project="+ SdChargecodeList.get(0).getProject() +" || BS,CS DocumentNo="+ SdChargecodeList.get(0).getDocumentNo() +" ] Size="+ CommonService.getSize(SdChargecodeList) +" --->");
							    }

								//Receipted Send to SAP
								new FiArreceiptreOutbService().SendingToSAP(rcvdLst);
								System.out.println("Step 3# Sending Receipted [Company="+ rcvdLst.get(0).getCompany() +" || Project="+ rcvdLst.get(0).getProject() +" || ReceiptedDocNo=" + rcvdLst.get(0).getReceiptDocNo() + " Size=" + CommonService.getSize(rcvdLst) +"] to SAP  --->");
							}
						}catch(Exception e){
							e.printStackTrace();
							System.err.println(e.getMessage());
						}
					}
			}			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());			
			System.err.println(e.getMessage());	
			throw e;
		}finally{
			System.out.println("End Sales Booking or Contract Processing ...newProcessing ---> ");
		}
	}

	@Deprecated
	/**
	 * Sales, Customer, Receipted, Unit send to SAP
	 * @throws Exception
	 */
	private void processing() throws Exception {
		try{
			
			String company = "SP";
			String project = "SPLCITY-SRI";
			String BookingOrContarctNo = "BS-1205-000016"; //  "CS-1204-000009-0" // "BS-1204-000007"; //BS-1204-000016
			String status = "A";
			String syncIsstaging = "Y"; //Can set value to [null]
			String documentType = "BS"; //CS //BS
			String issync = "N";
			String buildingPhase = null;
			String floorZone  = null;
			String unitNumber  = null;
			String instanceNo = null;
			
			//PROMIS Staging
			System.out.println("Start Create to PROMIS Staging===========================================>");
			List<Object> r_lst = new VwSyncMktBookConstructionAction().CreateToStaging(company, project, BookingOrContarctNo, status, syncIsstaging);
			System.out.println("End Create to PROMIS Staging===========================================>");
			
			
			//Sending to SAP
			if(r_lst.size() > 0)
			{
				buildingPhase = CommonService.IsNullRString(r_lst.get(3));
				floorZone = CommonService.IsNullRString(r_lst.get(4));
				unitNumber = CommonService.IsNullRString(r_lst.get(2));
				instanceNo = CommonService.IsNullRString(r_lst.get(5));
				
				//Customer
				System.out.println("Start Customer Sending  to SAP===========================================>");			
				VwSyncMktCustomerService vwSyncMktCustomerService = new VwSyncMktCustomerService();
				List<String> cus_lst = (List)r_lst.get(0);
				if(cus_lst != null)
				{
					for(String customerCode : cus_lst)
					{
						System.out.println("customerCode : " + customerCode);
						vwSyncMktCustomerService.SendingToSAP(customerCode, "N");
					}
				}
				System.out.println("Customer Sending  to SAP===========================================>");
				
				//Sales Booking or Sales Contract
				VwSyncMktCustBookContService vwSyncMktCustBookContService = new VwSyncMktCustBookContService();
				vwSyncMktCustBookContService.SendingToSAP(company, project, documentType, BookingOrContarctNo, status, instanceNo, issync);
				System.out.println("Sales Booking or Sales Contract Sending  to SAP===========================================>");
				
				//Property Unit
				PsWbsMasterService psWbsMasterService = new PsWbsMasterService();
				psWbsMasterService.SendingToSAP(company, project, buildingPhase, floorZone, unitNumber, instanceNo, issync);
				System.out.println("Property Unit Sending  to SAP===========================================>");
				
				//Receipted
				FiArreceiptreOutbService fiArreceiptreOutbService = new FiArreceiptreOutbService();
				fiArreceiptreOutbService.SendingToSAP(company, project, BookingOrContarctNo, status, instanceNo, issync);
				System.out.println("Receipted Sending to SAP  ================================================> ");
			}
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}

	public void ProcessingAll() throws Exception {
    	try{
    		
    		try{
    			String company = "SP";
    			
    			List<String> lst = new ArrayList<String>();
    			lst.add("BY");
    			lst.add("CGVA");
    			lst.add("CH3A1");
    			lst.add("CH3A2");
    			lst.add("CH3A3");
    			lst.add("CH3A4");
    			lst.add("CH3A5");
    			lst.add("CH3A6");
    			lst.add("CH3CT");
    			lst.add("CH4");
    			lst.add("CHR");
    			lst.add("CHS");
    			lst.add("CRF1");
    			lst.add("CRF2");
    			lst.add("CRRH");
    			lst.add("CRV1");
    			lst.add("CRV2");
    			lst.add("EX");
    			lst.add("NVOL");
    			lst.add("OP3-3");
    			lst.add("PO");
    			lst.add("PT");
    			lst.add("PVD");
    			lst.add("RP");
    			lst.add("SCH");
    			lst.add("SCR-C");
    			lst.add("SCR-R");
    			lst.add("SCR-T");
    			lst.add("SGV");
    			lst.add("SGV-L");
    			lst.add("SGV-P");
    			lst.add("SGV5");
    			lst.add("SGVJL");
    			lst.add("SGVP");
    			lst.add("SH");
    			lst.add("SNVTP");
    			lst.add("SP-AP");
    			lst.add("SP-DR");
    			lst.add("SP-S");
    			lst.add("SPET");
    			lst.add("SPK-A");
    			lst.add("SPK-K");
    			lst.add("SPK-P");
    			lst.add("SPK-R");
    			lst.add("SPK-T");
    			lst.add("SPKRP");
    			lst.add("SPKW");
    			lst.add("SPP-S");
    			lst.add("SPPC");
    			lst.add("SPR");
    			lst.add("SPV-P");
    			lst.add("SPVOR");
    			lst.add("SPVR5");
    			lst.add("SPVRS");
    			lst.add("SRP");
    			lst.add("SRS");
    			lst.add("SSL");
    			lst.add("SV-PC");
    			lst.add("SV-RB");
    			lst.add("SV-SW");
    			lst.add("SV5");
    			lst.add("SVO-L");
    			lst.add("SVP");
    			lst.add("SVP-N");
    			lst.add("SVP52");
    			lst.add("SVSP");
    			lst.add("SVT");
    			lst.add("SW");
    			lst.add("VCM");
    			lst.add("VP");
    			lst.add("VP48");
    			lst.add("VPS");
    			lst.add("VRAM");
    			lst.add("VRK");
    			lst.add("VSK");
    			lst.add("VSP");
    			
    			String docType = "BS";
    			
    			for(String project : lst){
    				
    				List<Object[]> lst1 = new VwSyncMktBookConstructionAction().findAllByProject(company, project, docType);
    				if(lst != null){
    					for(Object[] value : lst1 ){
    						
    						String BookingOrContarctNo = CommonService.IsNullRNull(value[2]);
    						String status = CommonService.IsNullRNull(value[3]);
    						String syncIsstaging = CommonService.IsNullRNull(value[4]);
    						String issync = "N";
    						
    					    new SalesBookingSingleProcessing().Processing(company, project, BookingOrContarctNo, status, syncIsstaging, issync);
    					    
    					}
    				}
    			}
    			
    		}catch(Exception e){
    			log.error(e.getMessage());
    			System.err.println(e.getMessage());
    		}
    		
    	}catch(Exception e){
    		log.error(e.getMessage());
			System.err.println(e.getMessage());
    		throw e;
    	}
    }


	public static void main(String[] args) {
		// TODO Auto-generated method stub		
		try{
			
			//Booking
			String company = "SP";
			String project = "SGV5"; //"SPV-P"; //SPLCITY-SRI
			String BookingOrContarctNo = "CS-1206-000156-0"; //"BS-1210-000171"; //  "CS-1204-000009-0" // "BS-1204-000007"; //BS-1204-000016
			String ReceipteddocumentNo = "RR-1210-000286";  //"RR-1210-000363";
			String receipt_status = "A";
			String syncIsstaging = "Y"; //Can set value to [null]
			String issync = "N";
			
			new SalesBookingProcessing().newProcessing(company, project, BookingOrContarctNo, ReceipteddocumentNo, receipt_status, null, syncIsstaging, issync);
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}

}
